package main.java.DMDE.model;

import java.util.*;

import main.java.DMDE.model.relationships.Relationship;
/**
 * Class Diagram
 */
public class Diagram {

    private String name;

    private List<Entity> entities;
    private List<Relationship> relationships;
    private List<Diagram> subDiagrams;

    public Diagram(String name) {
	setEntities(new ArrayList<Entity>());
	relationships = new ArrayList<Relationship>();
	subDiagrams = new ArrayList<Diagram>();
	this.name = name;
    };

    public Diagram() {
	setEntities(new ArrayList<Entity>());
	relationships = new ArrayList<Relationship>();
	subDiagrams = new ArrayList<Diagram>();
    }

    public boolean equals(Object diagram) {
	return diagram instanceof Diagram
		&& this.name.equals(((Diagram) diagram).name);
    }

    public List<Entity> getEntities() {
	return entities;
    }

    public Entity getEntityById(UUID id) {
    	for(Entity ent : entities) {
    	    if (ent.getId().equals(id))
    	    	return ent;
    	}
    	return null;
    }
    
    public void setEntities(List<Entity> entities) {
	this.entities = entities;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getName() {
	return name;
    }

    public Entity getEntityWithName(String entityName) {
	for(Entity ent : entities){
	    if(ent.getName().equals(entityName))
		return ent;
	}
	return null;
    }

    public List<Relationship> getRelationships() {
	return relationships;
    }

    public void addSubDiagram(Diagram subDiagram) {
	if (subDiagram == null) return;
	this.subDiagrams.add(subDiagram);
    }

    public void addEntity(Entity entity) {
	if (entity == null) return;
	this.entities.add(entity);
    }
    
    public void addRelationship(Relationship relationship) {
    	if (relationship == null) return;
	this.relationships.add(relationship);
    }
    
	public List<Diagram> getSubDiagrams() {
		return subDiagrams;
	}

	public List<String> getSubDiagramNames() {
	    List<String> names = new ArrayList<String>();
	    for(Diagram diagram: subDiagrams)
		names.add(diagram.getName());
	    return names;
	}

}
